package com.kehao.leetcode.hard.array;


import org.junit.Test;

/**
 * 视频讲解： https://www.bilibili.com/video/BV1aV411q7DM/?spm_id_from=333.788.recommend_more_video.-1
 */
public class FindDuplicateSolution {
    public int findDuplicate(int[] nums) {
        if(nums==null||nums.length==0) return 0;

        int fast = nums[nums[0]];
        int slow = nums[0];
        while (nums[fast]!=nums[slow]){
            fast = nums[nums[fast]];
            slow = nums[slow];
        }
        fast=0;
        while (nums[fast]!=nums[slow]){
            slow = nums[slow];
            fast = nums[fast];
        }

        return nums[fast];
    }

    @Test
    public void test01(){
        int[] nums = {1,3,4,2,2};
        int result = findDuplicate(nums);
        System.out.println(result);
    }

    @Test
    public void test02(){
        int[] nums = {3,1,3,4,2};
        int result = findDuplicate(nums);
        System.out.println(result);
    }

    @Test
    public void test03(){
        int[] nums = {1,1};
        int result = findDuplicate(nums);
        System.out.println(result);
    }

    @Test
    public void test04(){
        int[] nums = {1,1,2};
        int result = findDuplicate(nums);
        System.out.println(result);
    }

}
